<template>
  <d2-container>
    <el-table :data="list"
              border
              :resizable="false"
              stripe>
      <el-table-column label="图片"
                       prop="images">
        <template slot-scope="{ row }">
          <template v-if="!row.images">
            暂无
          </template>
          <template v-else>
            <el-image
              v-for="(item,index) in row.images"
              :key="index"
              fit="cover"
              :src="item.url"
              class="fit-size"
              :preview-src-list="images(row)"
            ></el-image>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="创建时间"
                       prop="time_create">
        <template slot-scope="{ row }">
          {{row.time_create | timeFilter}}
        </template>
      </el-table-column>
      <el-table-column label="分类"
                       prop="status">
        <template slot-scope="{ row }">
          {{isShare(row) ? '分享' : '海报'}}
        </template>
      </el-table-column>
      <el-table-column fixed="right"
                       label="操作"
                       width="145">
        <template slot-scope="{row}">
          <template v-if="isShare(row)">
            <el-button type="text"
                       size="small" @click="openMain(row)">上传主图
            </el-button>
          </template>
          <template v-else>
            <el-button type="text"
                       size="small" @click="openPoster(row)">上传海报
            </el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <poster ref="poster"></poster>
    <main-dialog ref="main"></main-dialog>
  </d2-container>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'
import commonCurd from '@/mixin/common-curd'
import poster from './components/poster'
import mainDialog from './components/main'

export default {
  name: 'image',
  data () {
    return {
    }
  },
  mixins: [commonCurd],
  async asyncData ({ store, route }) {
    return store.dispatch('setting/image/fetchList', {
      page: 1,
      limit: 12
    })
  },
  components: {
    poster, mainDialog
  },
  computed: {
    ...mapGetters('setting/image', ['list', 'total'])
  },
  methods: {
    ...mapActions('setting/image', ['fetchList']),
    isShare ({ type }) {
      return type === 'share'
    },
    async openPoster (row) {
      await this.$refs.poster.open(row)
      this.fetch()
    },
    async openMain (row) {
      await this.$refs.main.open(row)
      this.fetch()
    },
    images (row) {
      const { images } = row
      console.log(images)
      if (!images) {
        return []
      }
      return images.map(({ url }) => url)
    }
  }
}
</script>
<style lang="scss" scoped>
.fit-size {
  height: 80px;
  display: block;
  margin: 10px 0;
}
</style>
